package com.kuangjia.student_ems.controller;

import com.kuangjia.student_ems.dto.request.SendEmailCodeRequest;
import com.kuangjia.student_ems.dto.request.VerifyEmailCodeRequest;
import com.kuangjia.student_ems.service.EmailCodeService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 邮箱验证码控制器
 */
@RestController
@RequestMapping("/api/email-code")
public class EmailCodeController {

    private static final Logger logger = LoggerFactory.getLogger(EmailCodeController.class);

    @Autowired
    private EmailCodeService emailCodeService;

    /**
     * 发送邮箱验证码
     */
    @PostMapping("/send")
    public ResponseEntity<Map<String, Object>> sendEmailCode(@Valid @RequestBody SendEmailCodeRequest request) {
        try {
            boolean success = emailCodeService.sendEmailCode(request.getEmail());
            
            Map<String, Object> response = new HashMap<>();
            if (success) {
                response.put("success", true);
                response.put("message", "验证码发送成功");
                logger.info("邮箱 {} 验证码发送成功", request.getEmail());
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "验证码发送失败，请稍后重试");
                logger.warn("邮箱 {} 验证码发送失败", request.getEmail());
                return ResponseEntity.badRequest().body(response);
            }
        } catch (Exception e) {
            logger.error("发送验证码时发生异常", e);
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "验证码发送失败");
            return ResponseEntity.badRequest().body(response);
        }
    }

    /**
     * 验证邮箱验证码
     */
    @PostMapping("/verify")
    public ResponseEntity<Map<String, Object>> verifyEmailCode(@Valid @RequestBody VerifyEmailCodeRequest request) {
        try {
            boolean valid = emailCodeService.verifyEmailCode(request.getEmail(), request.getCode());
            
            Map<String, Object> response = new HashMap<>();
            if (valid) {
                response.put("success", true);
                response.put("message", "验证码验证成功");
                logger.info("邮箱 {} 验证码验证成功", request.getEmail());
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "验证码错误或已过期");
                logger.warn("邮箱 {} 验证码验证失败", request.getEmail());
                return ResponseEntity.badRequest().body(response);
            }
        } catch (Exception e) {
            logger.error("验证验证码时发生异常", e);
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "验证码验证失败");
            return ResponseEntity.badRequest().body(response);
        }
    }

    /**
     * 检查是否可以重新发送验证码
     */
    @GetMapping("/can-resend")
    public ResponseEntity<Map<String, Object>> canResendEmailCode(@RequestParam String email) {
        try {
            boolean canResend = emailCodeService.canResendEmailCode(email);
            
            Map<String, Object> response = new HashMap<>();
            response.put("canResend", canResend);
            response.put("message", canResend ? "可以发送验证码" : "请稍后再试");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("检查重发状态时发生异常", e);
            Map<String, Object> response = new HashMap<>();
            response.put("canResend", false);
            response.put("message", "检查失败");
            return ResponseEntity.badRequest().body(response);
        }
    }

    /**
     * 测试邮箱配置
     */
    @PostMapping("/test")
    public ResponseEntity<Map<String, Object>> testEmail(@RequestParam String email) {
        try {
            boolean success = emailCodeService.sendEmailCode(email);
            
            Map<String, Object> response = new HashMap<>();
            if (success) {
                response.put("success", true);
                response.put("message", "测试邮件发送成功");
                logger.info("测试邮件发送成功: {}", email);
            } else {
                response.put("success", false);
                response.put("message", "测试邮件发送失败");
                logger.warn("测试邮件发送失败: {}", email);
            }
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("测试邮件发送异常", e);
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "测试邮件发送失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
} 